﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace WeChatDataSrc
{
    /// <summary>
    /// 雷达图获取及上传
    /// </summary>
   public static class RadarImage
    {
       static string Dir = ConfigurationSettings.AppSettings["RadarImgDirectory"];
       static string User = ConfigurationSettings.AppSettings["RadarImgUser"];
       static string Pwd = ConfigurationSettings.AppSettings["RadarImgPwd"];
       static string URL = ConfigurationSettings.AppSettings["UploadRadarImgURL"];

      static RemoteHost host = new RemoteHost(Dir, User, Pwd);

      public static IOrderedEnumerable<FileInfo> GetZeroFivePath(DateTime SD)
       {
           SD = SD.AddHours(-8);
           DirectoryInfo info = new DirectoryInfo(Dir);
           IOrderedEnumerable<FileInfo> NewestFile = info.GetFiles(SD.ToString("yyyyMMdd") + "."+SD.ToString("HHmm") + "??.01*").OrderBy(n => n.Name);
           //FileInfo NewestFile = info.GetFiles("20150304*").OrderBy(n => n.Name).Last();
           return NewestFile;
       }
      public static IOrderedEnumerable<FileInfo> GetOneFivePath(DateTime SD)
       {
           SD = SD.AddHours(-8);
           DirectoryInfo info = new DirectoryInfo(Dir);
           IOrderedEnumerable<FileInfo> NewestFile = info.GetFiles(SD.ToString("yyyyMMdd") + "."+SD.ToString("HHmm")+"??.02*").OrderBy(n => n.Name);
           //FileInfo NewestFile = info.GetFiles("20150304*").OrderBy(n => n.Name).Last();
           return NewestFile;
       }

       public static void UploadImage(DateTime? sd)
       {
           DateTime NowMaxDateTime;

           if (DateTime.TryParse(ReadMax.ReadMaxRadarFore(), out NowMaxDateTime) == false)
           {
               NowMaxDateTime = DateTime.Now;
           }
           else
           {
               NowMaxDateTime = NowMaxDateTime.AddMinutes(6);
           }

           if (sd.HasValue)
           {
               NowMaxDateTime = Convert.ToDateTime(sd);
           }

           host.Connect();
           if (true)
           {
               DateTime Time = NowMaxDateTime;
               while (true)
               {
                   if (Time.CompareTo(DateTime.Now) > 0)
                   {
                       break;
                   }
                   try
                   {
                       IOrderedEnumerable<FileInfo> ImgPath = GetZeroFivePath(Time);
                       foreach (var obj in ImgPath)
                       {
                           string result = HttpPost.UploadFile(URL, obj.FullName);//上传文件
                       }
                       IOrderedEnumerable<FileInfo> OneFive = GetOneFivePath(Time);
                       foreach (var obj in OneFive)
                       {
                           string result = HttpPost.UploadFile(URL, obj.FullName);//上传文件
                       }
                   }
                   catch
                   {
                       Time = Time.AddMinutes(6); continue; 
                   }

                   Time = Time.AddMinutes(6);
               }
               host.Disconnect();
               return;
           }
       }
    }
}
